/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.InternalEList;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.DeploymentPlan;
import org.omg.deployment.InstanceDeploymentDescription;
import org.omg.deployment.InstanceResourceDeploymentDescription;
import org.omg.deployment.MonolithicDeploymentDescription;
import org.omg.deployment.PlanConnectionDescription;
import org.omg.deployment.PlanSubcomponentPortEndpoint;
import org.omg.deployment.Property;
import org.omg.xmi.impl.XMIImpl;

import com.zeligsoft.omg.lwccm.deployment.DeploymentUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Instance Deployment Description</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getName <em>Name</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getNode <em>Node</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getSource <em>Source</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getImplementation <em>Implementation</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getConfigProperty <em>Config Property</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getDeployedResource <em>Deployed Resource</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getDeployedSharedResource <em>Deployed Shared Resource</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getImplementationRef <em>Implementation Ref</em>}</li>
 *   <li>{@link org.omg.deployment.impl.InstanceDeploymentDescriptionImpl#getConnections <em>Connections</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class InstanceDeploymentDescriptionImpl extends XMIImpl implements
		InstanceDeploymentDescription {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The default value of the '{@link #getNode() <em>Node</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNode()
	 * @generated
	 * @ordered
	 */
	protected static final String NODE_EDEFAULT = null;

	/**
	 * The default value of the '{@link #getSource() <em>Source</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSource()
	 * @generated
	 * @ordered
	 */
	protected static final String SOURCE_EDEFAULT = null;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected InstanceDeploymentDescriptionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return DeploymentPackage.eINSTANCE.getInstanceDeploymentDescription();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return (String) getGroup()
				.get(DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_Name(),
						true);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		((FeatureMap.Internal) getGroup()).set(DeploymentPackage.eINSTANCE
				.getInstanceDeploymentDescription_Name(), newName);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getNode() {
		return (String) getGroup()
				.get(DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_Node(),
						true);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNode(String newNode) {
		((FeatureMap.Internal) getGroup()).set(DeploymentPackage.eINSTANCE
				.getInstanceDeploymentDescription_Node(), newNode);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getSource() {
		return (String) getGroup()
				.get(DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_Source(),
						true);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSource(String newSource) {
		((FeatureMap.Internal) getGroup()).set(DeploymentPackage.eINSTANCE
				.getInstanceDeploymentDescription_Source(), newSource);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public MonolithicDeploymentDescription getImplementation() {
		return (MonolithicDeploymentDescription) getGroup()
				.get(DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_Implementation(),
						true);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetImplementation(
			MonolithicDeploymentDescription newImplementation,
			NotificationChain msgs) {
		return ((FeatureMap.Internal) getGroup()).basicAdd(
				DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_Implementation(),
				newImplementation, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setImplementation(
			MonolithicDeploymentDescription newImplementation) {
		((FeatureMap.Internal) getGroup()).set(DeploymentPackage.eINSTANCE
				.getInstanceDeploymentDescription_Implementation(),
				newImplementation);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Property> getConfigProperty() {
		return getGroup().list(
				DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_ConfigProperty());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InstanceResourceDeploymentDescription> getDeployedResource() {
		return getGroup().list(
				DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_DeployedResource());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InstanceResourceDeploymentDescription> getDeployedSharedResource() {
		return getGroup()
				.list(DeploymentPackage.eINSTANCE
						.getInstanceDeploymentDescription_DeployedSharedResource());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public MonolithicDeploymentDescription getImplementationRef() {
		DeploymentPlan dp = null;
		MonolithicDeploymentDescription result = null;

		if (this.getImplementation() != null
				&& !this.getImplementation().getIdref().isEmpty()) {
			for (EObject next = this; next != null && dp == null; next = next
					.eContainer()) {
				if (next instanceof DeploymentPlan) {
					dp = (DeploymentPlan) next;
					break;
				}
			}
			if (dp != null) {
				result = dp.findImplementation(this.getImplementation()
						.getIdref());
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public MonolithicDeploymentDescription basicGetImplementationRef() {
		// TODO: implement this method to return the 'Implementation Ref' reference
		// -> do not perform proxy resolution
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	private EList<PlanConnectionDescription> connections = null;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<PlanConnectionDescription> getConnections() {
		if (connections == null) {
			connections = new BasicEList<PlanConnectionDescription>();

			final DeploymentPlan plan = DeploymentUtil.INSTANCE
					.getDeploymentPlan(this);
			if (plan != null) {
				for (PlanConnectionDescription nextConnection : plan
						.getConnection()) {
					for (PlanSubcomponentPortEndpoint end : nextConnection
							.getInternalEndpoint()) {
						if (end.getInstanceRef() == this) {
							connections.add(nextConnection);
							break;
						}
					}
				}
			}

		}

		return ECollections.unmodifiableEList(connections);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION:
			return basicSetImplementation(null, msgs);
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONFIG_PROPERTY:
			return ((InternalEList<?>) getConfigProperty()).basicRemove(
					otherEnd, msgs);
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_RESOURCE:
			return ((InternalEList<?>) getDeployedResource()).basicRemove(
					otherEnd, msgs);
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_SHARED_RESOURCE:
			return ((InternalEList<?>) getDeployedSharedResource())
					.basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NAME:
			return getName();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NODE:
			return getNode();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__SOURCE:
			return getSource();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION:
			return getImplementation();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONFIG_PROPERTY:
			return getConfigProperty();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_RESOURCE:
			return getDeployedResource();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_SHARED_RESOURCE:
			return getDeployedSharedResource();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION_REF:
			if (resolve)
				return getImplementationRef();
			return basicGetImplementationRef();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONNECTIONS:
			return getConnections();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NAME:
			setName((String) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NODE:
			setNode((String) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__SOURCE:
			setSource((String) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION:
			setImplementation((MonolithicDeploymentDescription) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONFIG_PROPERTY:
			getConfigProperty().clear();
			getConfigProperty().addAll(
					(Collection<? extends Property>) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_RESOURCE:
			getDeployedResource().clear();
			getDeployedResource()
					.addAll((Collection<? extends InstanceResourceDeploymentDescription>) newValue);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_SHARED_RESOURCE:
			getDeployedSharedResource().clear();
			getDeployedSharedResource()
					.addAll((Collection<? extends InstanceResourceDeploymentDescription>) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NAME:
			setName(NAME_EDEFAULT);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NODE:
			setNode(NODE_EDEFAULT);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__SOURCE:
			setSource(SOURCE_EDEFAULT);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION:
			setImplementation((MonolithicDeploymentDescription) null);
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONFIG_PROPERTY:
			getConfigProperty().clear();
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_RESOURCE:
			getDeployedResource().clear();
			return;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_SHARED_RESOURCE:
			getDeployedSharedResource().clear();
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NAME:
			return NAME_EDEFAULT == null ? getName() != null : !NAME_EDEFAULT
					.equals(getName());
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__NODE:
			return NODE_EDEFAULT == null ? getNode() != null : !NODE_EDEFAULT
					.equals(getNode());
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__SOURCE:
			return SOURCE_EDEFAULT == null ? getSource() != null
					: !SOURCE_EDEFAULT.equals(getSource());
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION:
			return getImplementation() != null;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONFIG_PROPERTY:
			return !getConfigProperty().isEmpty();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_RESOURCE:
			return !getDeployedResource().isEmpty();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__DEPLOYED_SHARED_RESOURCE:
			return !getDeployedSharedResource().isEmpty();
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__IMPLEMENTATION_REF:
			return basicGetImplementationRef() != null;
		case DeploymentPackage.INSTANCE_DEPLOYMENT_DESCRIPTION__CONNECTIONS:
			return !getConnections().isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //InstanceDeploymentDescriptionImpl
